package com.zjcloud.academic.service.impl;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zjcloud.academic.mapper.AcCourseScheduleMapper;
import com.zjcloud.academic.domain.AcCourseSchedule;
import com.zjcloud.academic.service.IAcCourseScheduleService;
import com.zjcloud.academic.service.IntelligentScheduleService;
import com.zjcloud.common.core.text.Convert;
import com.zjcloud.academic.service.IntelligentScheduleService.ScheduleStatistics;

/**
 * 课表管理Service业务层处理
 * 
 * @author zjcloud
 * @date 2024-01-01
 */
@Service
public class AcCourseScheduleServiceImpl implements IAcCourseScheduleService 
{
    private static final Logger log = LoggerFactory.getLogger(AcCourseScheduleServiceImpl.class);
    
    @Autowired
    private AcCourseScheduleMapper acCourseScheduleMapper;
    
    @Autowired
    private IntelligentScheduleService intelligentScheduleService;

    /**
     * 查询课表管理
     * 
     * @param id 课表管理主键
     * @return 课表管理
     */
    @Override
    public AcCourseSchedule selectAcCourseScheduleById(Long id)
    {
        return acCourseScheduleMapper.selectAcCourseScheduleById(id);
    }

    /**
     * 查询课表管理列表
     * 
     * @param acCourseSchedule 课表管理
     * @return 课表管理
     */
    @Override
    public List<AcCourseSchedule> selectAcCourseScheduleList(AcCourseSchedule acCourseSchedule)
    {
        return acCourseScheduleMapper.selectAcCourseScheduleList(acCourseSchedule);
    }

    /**
     * 根据学期查询课表列表
     * 
     * @param semesterId 学期ID
     * @return 课表列表
     */
    @Override
    public List<AcCourseSchedule> selectAcCourseScheduleBySemesterId(Long semesterId)
    {
        return acCourseScheduleMapper.selectAcCourseScheduleBySemesterId(semesterId);
    }

    /**
     * 新增课表管理
     * 
     * @param acCourseSchedule 课表管理
     * @return 结果
     */
    @Override
    public int insertAcCourseSchedule(AcCourseSchedule acCourseSchedule)
    {
        // 检查课表冲突
        if (checkScheduleConflict(acCourseSchedule)) {
            throw new RuntimeException("课表存在冲突，请检查时间、教室安排");
        }
        return acCourseScheduleMapper.insertAcCourseSchedule(acCourseSchedule);
    }

    /**
     * 修改课表管理
     * 
     * @param acCourseSchedule 课表管理
     * @return 结果
     */
    @Override
    public int updateAcCourseSchedule(AcCourseSchedule acCourseSchedule)
    {
        // 检查课表冲突
        if (checkScheduleConflict(acCourseSchedule)) {
            throw new RuntimeException("课表存在冲突，请检查时间、教室安排");
        }
        return acCourseScheduleMapper.updateAcCourseSchedule(acCourseSchedule);
    }

    /**
     * 批量删除课表管理
     * 
     * @param ids 需要删除的课表管理主键
     * @return 结果
     */
    @Override
    public int deleteAcCourseScheduleByIds(Long[] ids)
    {
        return acCourseScheduleMapper.deleteAcCourseScheduleByIds(ids);
    }

    /**
     * 删除课表管理信息
     * 
     * @param id 课表管理主键
     * @return 结果
     */
    @Override
    public int deleteAcCourseScheduleById(Long id)
    {
        return acCourseScheduleMapper.deleteAcCourseScheduleById(id);
    }

    /**
     * 根据学期ID删除课表
     * 
     * @param semesterId 学期ID
     * @return 结果
     */
    @Override
    public int deleteAcCourseScheduleBySemesterId(Long semesterId)
    {
        return acCourseScheduleMapper.deleteAcCourseScheduleBySemesterId(semesterId);
    }

    /**
     * 根据日期范围查询课表列表
     * 
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param semesterId 学期ID
     * @return 课表列表
     */
    @Override
    public List<AcCourseSchedule> selectAcCourseScheduleByDateRange(String startDate, String endDate, Long semesterId)
    {
        return acCourseScheduleMapper.selectAcCourseScheduleByDateRange(startDate, endDate, semesterId);
    }

    /**
     * 检查课表冲突
     * 
     * @param acCourseSchedule 课表信息
     * @return 是否存在冲突
     */
    @Override
    public boolean checkScheduleConflict(AcCourseSchedule acCourseSchedule)
    {
        int conflictCount = acCourseScheduleMapper.checkScheduleConflict(acCourseSchedule);
        return conflictCount > 0;
    }

    /**
     * 生成排课任务
     * 
     * @param semesterId 学期ID
     * @param algorithmConfigId 算法配置ID
     * @return 生成结果
     */
    @Override
    public boolean generateCourseSchedule(Long semesterId, Long algorithmConfigId)
    {
        // 1. 删除该学期原有课表
        deleteAcCourseScheduleBySemesterId(semesterId);
        
        // 2. 调用智能排课服务生成课表
        try {
            List<AcCourseSchedule> result = intelligentScheduleService.intelligentSchedule(semesterId);
            return result != null && !result.isEmpty();
        } catch (Exception e) {
            log.error("生成排课任务失败：", e);
            return false;
        }
    }

    /**
     * 获取课表统计信息
     * 
     * @param semesterId 学期ID
     * @return 统计信息
     */
    @Override
    public ScheduleStatistics getScheduleStatistics(Long semesterId) {
        // 查询统计信息
        // 1. 总课程数 - 使用学期ID查询所有课程
        List<AcCourseSchedule> allCourses = acCourseScheduleMapper.selectAcCourseScheduleBySemesterId(semesterId);
        int totalCourses = allCourses.size();
        
        // 2. 已排课数 - 统计已分配教师的课程数
        int scheduledCourses = 0;
        for (AcCourseSchedule course : allCourses) {
            if (course.getTeacherId() != null) {
                scheduledCourses++;
            }
        }
        
        // 3. 冲突数 - 使用已有的冲突检测方法
        int conflictCount = 0;
        for (AcCourseSchedule course : allCourses) {
            if (course.getTeacherId() != null) { // 只检查已排课的课程
                int conflicts = acCourseScheduleMapper.checkScheduleConflict(course);
                conflictCount += conflicts;
            }
        }
        
        // 4. 利用率
        double utilizationRate = totalCourses > 0 ? (double) scheduledCourses / totalCourses : 0;
        
        // 创建统计信息对象
        IntelligentScheduleService.ScheduleStatistics statistics = new IntelligentScheduleService.ScheduleStatistics();
        statistics.setTotalCourses(totalCourses);
        statistics.setScheduledCourses(scheduledCourses);
        statistics.setConflictCount(conflictCount);
        statistics.setUtilizationRate(utilizationRate);
        
        return statistics;
    }
}